<template>
  <div :style='{"padding":" 30px 20px","flexWrap":"wrap","background":"url(https://img.freepik.com/free-photo/stack-books-library_23-2147845946.jpg) no-repeat center center / cover, #f8f4e9","display":"flex","position":"relative","minHeight":"100vh"}'>
    <div class="overlay" :style='{"position":"absolute","top":"0","left":"0","width":"100%","height":"100%","background":"rgba(139, 69, 19, 0.05)","zIndex":"1"}'></div>
    
    <el-form
	  :style='{"padding":"30px 40px","boxShadow":"0 0px 10px rgba(139, 69, 19, 0.1)","borderRadius":"10px","flexWrap":"wrap","background":"rgba(255, 255, 240, 0.95)","display":"flex","justifyContent":"space-between","zIndex":"2","position":"relative"}'
      class="add-update-preview"
      ref="ruleForm"
      :model="ruleForm"
      label-width="150px"
    >  
     <el-row>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'   v-if="flag=='yonghu'"  label="用户名" prop="yonghuming">
          <el-input v-model="ruleForm.yonghuming" readonly              placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'   v-if="flag=='yonghu'"  label="姓名" prop="xingming">
          <el-input v-model="ruleForm.xingming"               placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}' v-if="flag=='yonghu'"  label="性别" prop="xingbie">
          <el-select v-model="ruleForm.xingbie"  placeholder="请选择性别">
            <el-option
                v-for="(item,index) in yonghuxingbieOptions"
                v-bind:key="index"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'   v-if="flag=='yonghu'"  label="联系方式" prop="lianxifangshi">
          <el-input v-model="ruleForm.lianxifangshi"               placeholder="联系方式" clearable></el-input>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}' v-if="flag=='yonghu'" label="头像" prop="touxiang">
          <file-upload
          tip="点击上传头像"
          action="file/upload"
          :limit="3"
          :multiple="true"
          :fileUrls="ruleForm.touxiang?ruleForm.touxiang:''"
          @change="yonghutouxiangUploadChange"
          ></file-upload>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'   v-if="flag=='chiwuzhe'"  label="持书账号" prop="chiwuzhanghao">
          <el-input v-model="ruleForm.chiwuzhanghao" readonly              placeholder="持书账号" clearable></el-input>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'   v-if="flag=='chiwuzhe'"  label="持书姓名" prop="chiwuxingming">
          <el-input v-model="ruleForm.chiwuxingming"               placeholder="持书姓名" clearable></el-input>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}' v-if="flag=='chiwuzhe'"  label="性别" prop="xingbie">
          <el-select v-model="ruleForm.xingbie"  placeholder="请选择性别">
            <el-option
                v-for="(item,index) in chiwuzhexingbieOptions"
                v-bind:key="index"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}'   v-if="flag=='chiwuzhe'"  label="联系电话" prop="lianxidianhua">
          <el-input v-model="ruleForm.lianxidianhua"               placeholder="联系电话" clearable></el-input>
        </el-form-item>
		<el-form-item :style='{"padding":"0px","boxShadow":"0px 0px 0px 0px #d2b3aa","margin":"0 0 20px 0","borderColor":"#d2b48c","borderRadius":"0px","background":"#fff","borderWidth":"0px","display":"block","width":"100%","minWidth":"500px","borderStyle":"solid"}' v-if="flag=='users'" label="用户名" prop="username">
			<el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
		</el-form-item>
		<el-form-item :style='{"width":"100%","padding":"0","margin":"0 auto","background":"none"}'>
			<el-button :style='{"border":"1px solid #d2b48c","cursor":"pointer","padding":"0 16px","boxShadow":"inset 0px 0px 56px 0px #f8f4e9","margin":"20px 40px 20px 0","color":"#8b4513","minWidth":"120px","outline":"none","borderRadius":"4px","background":"#fff","width":"auto","lineHeight":"44px","fontSize":"14px","height":"44px"}' type="primary" @click="onUpdateHandler">修 改</el-button>
		</el-form-item>
      </el-row>
    </el-form>
  </div>
</template>
<script>
// 数字，邮件，手机，url，身份证校验
import { isNumber,isIntNumer,isEmail,isMobile,isPhone,isURL,checkIdCard } from "@/utils/validate";

export default {
  data() {
    return {
      ruleForm: {},
      flag: '',
      usersFlag: false,
      yonghuxingbieOptions: [],
      chiwuzhexingbieOptions: [],
    };
  },
  mounted() {
    var table = this.$storage.get("sessionTable");
    this.flag = table;
    this.$http({
      url: `${this.$storage.get("sessionTable")}/session`,
      method: "get"
    }).then(({ data }) => {
      if (data && data.code === 0) {
        this.ruleForm = data.data;
      } else {
        this.$message.error(data.msg);
      }
    });
    this.yonghuxingbieOptions = "男,女".split(',')
    this.chiwuzhexingbieOptions = "男,女".split(',')
  },
  methods: {
    yonghutouxiangUploadChange(fileUrls) {
        this.ruleForm.touxiang = fileUrls;
    },
    onUpdateHandler() {
      if((!this.ruleForm.yonghuming)&& 'yonghu'==this.flag){
        this.$message.error('用户名不能为空');
        return
      }
      if((!this.ruleForm.xingming)&& 'yonghu'==this.flag){
        this.$message.error('姓名不能为空');
        return
      }
      if((!this.ruleForm.mima)&& 'yonghu'==this.flag){
        this.$message.error('密码不能为空');
        return
      }
      if( 'yonghu' ==this.flag && this.ruleForm.lianxifangshi&&(!isMobile(this.ruleForm.lianxifangshi))){
        this.$message.error(`联系方式应输入手机格式`);
        return
      }
        if(this.ruleForm.touxiang!=null) {
                this.ruleForm.touxiang = this.ruleForm.touxiang.replace(new RegExp(this.$base.url,"g"),"");
        }
      if( 'yonghu' ==this.flag && this.ruleForm.money&&(!isNumber(this.ruleForm.money))){
        this.$message.error(`余额应输入数字`);
        return
      }
      if((!this.ruleForm.chiwuzhanghao)&& 'chiwuzhe'==this.flag){
        this.$message.error('持书账号不能为空');
        return
      }
      if((!this.ruleForm.chiwuxingming)&& 'chiwuzhe'==this.flag){
        this.$message.error('持书姓名不能为空');
        return
      }
      if((!this.ruleForm.mima)&& 'chiwuzhe'==this.flag){
        this.$message.error('密码不能为空');
        return
      }
      if( 'chiwuzhe' ==this.flag && this.ruleForm.lianxidianhua&&(!isMobile(this.ruleForm.lianxidianhua))){
        this.$message.error(`联系电话应输入手机格式`);
        return
      }
      if( 'chiwuzhe' ==this.flag && this.ruleForm.money&&(!isNumber(this.ruleForm.money))){
        this.$message.error(`余额应输入数字`);
        return
      }
      if('users'==this.flag && this.ruleForm.username.trim().length<1) {
	this.$message.error(`用户名不能为空`);
        return	
      }
      this.$http({
        url: `${this.$storage.get("sessionTable")}/update`,
        method: "post",
        data: this.ruleForm
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "修改信息成功",
            type: "success",
            duration: 1500,
            onClose: () => {
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
	.el-date-editor.el-input {
		width: auto;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__label {
	  	  padding: 0 10px 0 0;
	  	  text-shadow: 0 1px 10px #fff;
	  	  color: #8b4513;
	  	  background: none;
	  	  display: inline-block;
	  	  width: 150px;
	  	  font-size: 14px;
	  	  line-height: 40px;
	  	  text-align: right;
	  	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__content {
	  margin-left: 150px;
	}
	
	.add-update-preview .el-input /deep/ .el-input__inner {
	  	  border: 1px solid #d2b48c;
	  	  border-radius: 4px;
	  	  padding: 0 12px;
	  	  box-shadow: inset 0px 0px 24px 0px #f5f0e6;
	  	  outline: none;
	  	  color: #666;
	  	  background: #fff;
	  	  width: 100%;
	  	  font-size: 14px;
	  	  height: 40px;
	  	}
	
	.add-update-preview .el-select /deep/ .el-input__inner {
	  	  border: 1px solid #d2b48c;
	  	  border-radius: 4px;
	  	  padding: 0 10px;
	  	  box-shadow: inset 0px 0px 24px 0px #f5f0e6;
	  	  outline: none;
	  	  color: #666;
	  	  background: #fff;
	  	  width: 100%;
	  	  font-size: 14px;
	  	  height: 40px;
	  	}
	
	.add-update-preview .el-date-editor /deep/ .el-input__inner {
	  	  border: 1px solid #d2b48c;
	  	  border-radius: 4px;
	  	  padding: 0 10px 0 30px;
	  	  box-shadow: inset 0px 0px 24px 0px #f5f0e6;
	  	  outline: none;
	  	  color: #666;
	  	  background: #fff;
	  	  width: 100%;
	  	  font-size: 14px;
	  	  height: 40px;
	  	}
	
	.add-update-preview /deep/ .el-upload--picture-card {
		background: transparent;
		border: 0;
		border-radius: 0;
		width: auto;
		height: auto;
		line-height: initial;
		vertical-align: middle;
	}
	
	.add-update-preview /deep/ .upload .upload-img {
	  	  width: 88px;
	  	  height: 88px;
	  	  overflow: hidden;
	  	}
	
	.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
	  	  width: 88px;
	  	  height: 88px;
	  	}
	
	.add-update-preview /deep/ .el-upload .upload-img img {
	  	  width: 88px;
	  	  height: 88px;
	  	}
	
	.add-update-preview /deep/ .el-upload-list .el-upload-list__item .el-icon-close {
		display: none;
	}
	
	.add-update-preview /deep/ .el-upload .upload-img .el-icon-plus {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 20px;
	}

	.add-update-preview /deep/ .el-pagination {
		padding: 0;
		margin: 0;
		display: flex;
		justify-content: center;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pagination__total {
		margin: 0 10px 0 0;
		color: #666;
		font-weight: 500;
	}
	
	.add-update-preview /deep/ .el-pagination .btn-prev {
		border: none;
		border-radius: 2px;
		margin: 0 5px;
		color: #666;
		background: #f4f4f5;
		height: 26px;
		width: 30px;
	}
	
	.add-update-preview /deep/ .el-pagination .btn-next {
		border: none;
		border-radius: 2px;
		margin: 0 5px;
		color: #666;
		background: #f4f4f5;
		height: 26px;
		width: 30px;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pager {
		padding: 0;
		margin: 0;
		display: flex;
		align-items: center;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pager .number {
		height: 26px;
		min-width: 30px;
		text-align: center;
		line-height: 26px;
		border-radius: 2px;
		margin: 0 5px;
		background: #f4f4f5;
		color: #666;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pager .number:hover {
		background: #8b4513;
		color: #fff;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pager .number.active {
		color: #fff;
		background: #8b4513;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pagination__sizes {
		margin: 0 10px 0 0;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pagination__sizes .el-input {
		margin: 0;
		width: 100px;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pagination__jump {
		margin: 0 0 0 10px;
		color: #666;
	}
	
	.add-update-preview /deep/ .el-pagination .el-pagination__editor {
		border: 1px solid #dcdfe6;
		border-radius: 3px;
		text-align: center;
		height: 26px;
		width: 50px;
		margin: 0 0 0 0;
		color: #666;
	}
</style>
